var itemID;
var token;
var favoriteIcon = document.getElementById("favoriteIcon");
var pageflag;

(function($) {

	setPxPerRem();
	mui.plusReady(function() {
		itemID = getQueryString("itemID");
		pageflag = getQueryString("pageflag");
		token = localStorage.getItem("token_type") + " " + localStorage.getItem("access_token");
		if(isHaveNet()) {
			plus.nativeUI.showWaiting();
			getPoilcyInfo(itemID);
		} else {
			mui.toast("网络连接失败，请连网后重试")
		}
	});

})();

function getProjectInfo(authorization, id) {
	var settings = {
		url: getRequestAddressUrl("testline") + "/zs_project/" + id,
		method: "GET",
		headers: {
			"accept": "application/json",
			"authorization": authorization
		}
	}

	$.ajax(settings).done(function(data) {
		if(!isEmpty(data)) {
			var row = data;
			hiddenEle(favoriteIcon);
			if(!isEmpty(row)) {
				document.getElementById("titleText").innerHTML = row.name;
				document.getElementById("title").innerHTML = row.name;
				if(!isEmpty(row.thumb)) {
					document.getElementById("titleImg").src = row.thumb;
				}
				if(!isEmpty(row.content)) {
					document.getElementById("policyContent").innerHTML = row.content;
				}

				if(!isEmpty(row.browsed)) {
					document.getElementById("browsed").innerHTML = row.browsed + "";
				} else {
					document.getElementById("browsed").innerHTML = "0";
				}

				if(!isEmpty(row.created_at)) {
					document.getElementById("createTime").innerHTML = (row.created_at).substr(0, 10);
				}
			}
		}
	});
}

function getPoilcyInfo(id) {
	var token = localStorage.getItem("token");

	if(isEmpty(token)) {
		mui.toast("登陆过期，请重新登陆");
		return;
	}

	var p = {
		op: "message_content",
		token: token,
		data: {
			id: id
		}
	};

	var postData = {
		p: JSON.stringify(p)
	}

	var opts = {
		url: '',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: postData
	}

	crossDomainAjax(opts, function(data) {
		plus.nativeUI.closeWaiting();
		if(!isEmpty(data)) {
			$("#titleText").html(data.title);
			$("#title").html(data.title);
			$("#policyContent").html(data.content);
			$("#createTime").html(data.add_time);
		} else {
			mui.toast("获取详情失败");
		}
	});
}

function getFavoriteType(authorization, id) {
	var settings = {
		url: getRequestAddressUrl("testline") + "/user/is_favorite/policy/" + id,
		method: "GET",
		headers: {
			"accept": "application/json",
			"authorization": authorization
		}
	}
	$.ajax(settings).done(function(data) {
		plus.nativeUI.closeWaiting();
		showEle(favoriteIcon);
		if(!isEmpty(data)) {
			var meta = data.meta;
			if(!isEmpty(meta)) {
				if(meta.code == "10010") {
					$("#favoriteIcon").attr('src', "../../ui/building/images/09policyinfo/02.png")
				} else {
					$("#favoriteIcon").attr('src', "../../ui/building/images/09policyinfo/01.png")
				}
			} else {
				$("#favoriteIcon").attr('src', "../../ui/building/images/09policyinfo/01.png")
			}
		} else {
			$("#favoriteIcon").attr('src', "../../ui/building/images/09policyinfo/01.png")
		}
	});
}
$("#favoriteIcon").click(function() {
	var srcPath = favoriteIcon.src;
	if(isHaveNet()) {
		if(srcPath.indexOf("09policyinfo/02.png") > -1) {
			deleteFavorite(token, itemID)
		} else {
			favoritePolicy(token, itemID);
		}
	} else {
		mui.toast("请检测网络连接");
	}

});

/**
 * 收藏政策
 * @param {Object} authorization
 * @param {Object} id
 */
function favoritePolicy(authorization, id) {
	var settings = {
		url: getRequestAddressUrl("testline") + "/user/favorite/policy",
		method: "POST",
		data: {
			"id": id
		},
		headers: {
			"accept": "application/json",
			"authorization": authorization
		}
	}

	$.ajax(settings).done(function(data) {
		if(!isEmpty(data)) {
			var meta = data.meta;
			if(!isEmpty(meta)) {
				if(meta.code == "10000") {
					mui.toast("政策收藏成功");
					$("#favoriteIcon").attr('src', "../../ui/building/images/09policyinfo/02.png")
				} else {
					mui.toast("政策收藏失败");
					$("#favoriteIcon").attr('src', "../../ui/building/images/09policyinfo/01.png")
				}
			} else {
				mui.toast("政策收藏失败");
				$("#favoriteIcon").attr('src', "../../ui/building/images/09policyinfo/01.png")
			}
		} else {
			mui.toast("政策收藏失败");
			$("#favoriteIcon").attr('src', "../../ui/building/images/09policyinfo/01.png")
		}
	});
}

function deleteFavorite(authorization, id) {
	var settings = {
		url: getRequestAddressUrl("testline") + "/user/favorite/policy/" + id,
		method: "POST",
		data: {
			"_method": "DELETE"
		},
		headers: {
			"accept": "application/json",
			"authorization": authorization
		}
	}

	$.ajax(settings).done(function(data) {
		if(!isEmpty(data)) {
			var meta = data.meta;
			if(!isEmpty(meta)) {
				if(meta.code == "10000") {
					mui.toast("已取消政策收藏");
					$("#favoriteIcon").attr('src', "../../ui/building/images/09policyinfo/01.png")
				} else {
					mui.toast("取消政策收藏失败");
					$("#favoriteIcon").attr('src', "../../ui/building/images/09policyinfo/02.png")
				}
			} else {
				mui.toast("取消政策收藏失败");
				$("#favoriteIcon").attr('src', "../../ui/building/images/09policyinfo/02.png")
			}
		} else {
			mui.toast("取消政策收藏失败");
			$("#favoriteIcon").attr('src', "../../ui/building/images/09policyinfo/02.png")
		}
	});
}